package com.bonc.modules.rpt.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonc.datasource.annotation.DataSource;
import com.bonc.factory.PageFactory;
import com.bonc.modules.rpt.dao.SunshineActionDao;
import com.bonc.modules.rpt.dao.SunshineActionTotDao;
import com.bonc.modules.rpt.entity.SunshineActionEntity;
import com.bonc.modules.rpt.entity.SunshineActionTotEntity;
import com.bonc.modules.rpt.param.SunshineActionParam;
import com.bonc.modules.rpt.service.SunshineActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author jinlong
 * @date 2021/8/4
 */
@Service
@DataSource("data")
public class SunshineActionServiceImpl extends ServiceImpl<SunshineActionDao, SunshineActionEntity> implements SunshineActionService {

    @Autowired
    private SunshineActionTotDao sunshineActionTotDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveBatch(Collection<SunshineActionEntity> entityList, int batchSize) {
        String sqlStatement = getSqlStatement(SqlMethod.INSERT_ONE);
        return executeBatch(entityList, batchSize, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
    }

    @Override
    public Page<SunshineActionEntity> page(SunshineActionParam suddenEventParam) {
        QueryWrapper<SunshineActionEntity> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(suddenEventParam)) {
            if (ObjectUtil.isNotEmpty(suddenEventParam.getDealMonth())) {
                queryWrapper.eq("to_char(deal_date ,'yyyy-MM' )", suddenEventParam.getDealMonth());
            }
        }
        queryWrapper.orderByDesc("deal_date");
        return this.baseMapper.selectPage(PageFactory.defaultPage(), queryWrapper);
    }

    @Override
    public List<SunshineActionEntity> list(SunshineActionParam suddenEventParam) {
        QueryWrapper<SunshineActionEntity> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(suddenEventParam)) {
            if (ObjectUtil.isNotEmpty(suddenEventParam.getDealMonth())) {
                queryWrapper.eq("to_char(deal_date ,'yyyy-MM' )", suddenEventParam.getDealMonth());
            }
        }
        queryWrapper.orderByDesc("deal_date");
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public int delete(SunshineActionParam suddenEventParam) {
        return this.baseMapper.deleteById(suddenEventParam.getId());
    }

    @Override
    public int calcCount() {
        return this.baseMapper.calcCount();
    }

    @Override
    public int calcMom() {
        return this.baseMapper.calcMom();
    }

    @Override
    public int calcDutyIncrease(){
        return this.baseMapper.calcDutyIncrease();
    }

    @Override
    public List<Map> totalList(SunshineActionParam sunshineActionParam) {
        QueryWrapper<SunshineActionTotEntity> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(sunshineActionParam)) {
            if (ObjectUtil.isNotEmpty(sunshineActionParam.getDealYear())) {
                queryWrapper.eq("substr(DEAL_MONTH,0,4)", sunshineActionParam.getDealYear());
            }
        }
        queryWrapper.orderByDesc("deal_month");
        List<SunshineActionTotEntity> sunshineActionTotEntities = this.sunshineActionTotDao.selectList(queryWrapper);


        List<Map> resultList = new ArrayList<>();
        List<String> cityList = sunshineActionTotEntities.stream().map(SunshineActionTotEntity::getCityName).distinct().collect(Collectors.toList());
        for (String cityName : cityList) {
            Map<String, Object> collect = new HashMap<>();
            if (sunshineActionParam.getIsDuty()) {
                collect = sunshineActionTotEntities.stream().filter(sunshineActionTotEntity -> StrUtil.equals(sunshineActionTotEntity.getCityName(), cityName))
                        .collect(Collectors.toMap(sunshineActionTotEntity -> sunshineActionTotEntity.getDealMonth().substring(4), SunshineActionTotEntity::getDutyCount));
            } else {
                collect = sunshineActionTotEntities.stream().filter(sunshineActionTotEntity -> StrUtil.equals(sunshineActionTotEntity.getCityName(), cityName))
                        .collect(Collectors.toMap(sunshineActionTotEntity -> sunshineActionTotEntity.getDealMonth().substring(4), SunshineActionTotEntity::getCount));
            }
            collect.put("cityName", cityName);
            resultList.add(collect);
        }

        return resultList;
    }
}
